package megrez.keycloak.providers.opt;

import java.util.Locale;

import lombok.experimental.UtilityClass;
import megrez.keycloak.common.Consts;
import megrez.keycloak.common.MailMessage;
import megrez.keycloak.providers.mqtt.MqttUtils;

/**
 * 工具
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@UtilityClass
public class MailUtils {
    /**
     * 创建邮件消息
     * 
     * @param code 验证码
     * @param ttl  过期时间
     * @param mail 邮件地址
     * @return
     */
    public MailMessage buildMailMessage(String code, int ttl, String mail, Locale locale) {
        MailMessage rslt = new MailMessage();

        rslt.setLocale(locale);
        rslt.addMail(mail);
        rslt.addParam("code", code)
                .addParam("ttl", String.valueOf(Math.floorDiv(ttl, 60)));
        rslt.setTemplateCode(Consts.MAIL_TEMPLATE_CODE_OTP);
        return rslt;
    }

    public String getMqttTopic(final String realmId) {
        String topic = Consts.MQTT_TOPIC_PREFIX_MAIL + "/" + realmId;
        return  MqttUtils.normalizeTopic(topic).toUpperCase();
    }
}
